Repository navigation
Conversation
This was referenced Oct 28, 2026
Collaborator
Author
|
replaced with !823 upstream |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
insertJobQueuekeeps the queue sorted by descending priority and finds the insertion point by walking the list from the head. A new job goes after every queued job of the same or a higher priority, so a job of the most common case (the same priority as the jobs already queued) walks the whole queue. When a burst of N jobs of one priority is queued before the drain starts, the inserts cost O(N²) link steps.This happens in practice when an async API produces one job per element. For example, a SQLite query through SQLiteNIO's future-based API makes one job per row, so a read of a few hundred thousand rows queues that many jobs before the drain runs them.
This PR keeps a pointer to the tail of the queue. The tail has the lowest priority in the queue, so a job whose priority is not higher than the tail's goes right after the tail in O(1). A job with a higher priority still scans from the head, as before. The order does not change: higher priority first, and FIFO within one priority.
1. Insert. If the queue has a tail and
newJob.rawPriority <= tail.rawPriority, link the job after the tail and make it the new tail. Otherwise use the existing scan, and set the tail when the scan appends at the end (which happens only when the queue was empty).2. Claim.
claimNextFromQueueclears the tail when it takes the last job.headJob,tailJobandnextInQueue()arefileprivate, and these two functions are the only places that change the links, so the tail cannot become stale.Measurements
A Swift/Wasm app (Khasm) that writes a stored building graph to SQLite again on reopen, release build, Node.js 22, medians:
In a CPU profile of the 210k-node write before this change, the job queue took about 11% of the time.
Testing
testTaskPriorityOrderWithMixedPriorities(XCTest on Node.js):The test also passes on
mainwithout this change, so the order is the same as before.make unittest